package com.kj.tms.common.bean.session;


import com.kj.tms.common.bean.exception.SessionException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Enumeration;

/**
 * Created by wzl on 2017/10/19.
 * session管理实现类
 */
@Service
public class SessionManager  {

    @Resource(name = "httpSessionImpl")
    private SessionHandle sessionHandle;

    /**
     * 设置session属性对
     *
     * @param name  key
     * @param value 值
     * @throws SessionException
     */
    public void setAttribute(String name, Object value) throws SessionException {
        sessionHandle.setAttribute(name, value);
    }

    /**
     * 根据属性key获取value
     *
     * @param name key
     * @return String 值
     * @throws SessionException
     */
    public Object getAttribute(String name) throws SessionException {
        return sessionHandle.getAttribute(name);
    }

    /**
     * 根据属性key删除属性对
     *
     * @param name key
     * @throws SessionException
     */
    public void removeAttribute(String name) throws SessionException {
        sessionHandle.removeAttribute(name);
    }

    /**
     * 获取全部属性对
     *
     * @return Enumeration 枚举数组
     * @throws SessionException
     */
    public Enumeration<?> getAttributeNames() throws SessionException {
        return sessionHandle.getAttributeNames();
    }

    /**
     * 使该session失效，删除sessionId下的所有name value
     *
     * @throws SessionException
     */
    public void invalidate() throws SessionException {
        sessionHandle.invalidate();
    }

    /**
     * 获取sessionId
     *
     * @return 获取sessionId
     */
    public String getId() {
        return sessionHandle.getId();
    }

    /**
     * 设置重置时间
     */
    public void expire() throws SessionException {
        sessionHandle.expire();
    }
}
